﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Eagles.Application.Model.DeliverGoods.Requset;
using Eagles.Base.Cache;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.Order;
using Eagles.DomainService.Model.User;
using Eagles.Interface.DataAccess;

namespace Eagles.DomianService.DataAccess
{
    public class OrderDataAccess : IOrderDataAccess
    {
        private readonly IDbManager dbManager;
        private readonly ICacheHelper cacheHelper;

        public OrderDataAccess(IDbManager dbManager, ICacheHelper cacheHelper)
        {
            this.dbManager = dbManager;
            this.cacheHelper = cacheHelper;
        }
        public List<TbOrder> GetOrderList(GetDeliverGoodsRequset requset,out int totalCount)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();



            //if (requset.OrgId > 0)
            //{
            //    parameter.Append(" and  OrgId = @OrgId ");
            //    dynamicParams.Add("OrgId", requset.OrgId);
            //}

            //if (requset.BranchId > 0)
            //{
            //    parameter.Append(" and BranchId = @BranchId ");
            //    dynamicParams.Add("BranchId", requset.BranchId);
            //}


            var token = cacheHelper.GetData<TbUserToken>(requset.Token);

            if (token.OrgId > 0)
            {
                parameter.Append(" and  a.OrgId = @OrgId ");
                dynamicParams.Add("OrgId", token.OrgId);
            }

            if (requset.StartTime != null)
            {
                parameter.Append(" and a.CreateTime >= @StartTime ");
                dynamicParams.Add("StartTime", requset.StartTime);
            }

            if (requset.EndTime != null)
            {
                parameter.Append(" and a.CreateTime <= @EndTime ");
                dynamicParams.Add("EndTime", requset.EndTime);
            }


            if (!string.IsNullOrWhiteSpace(requset.GoodsName))
            {
                parameter.Append(" and a.ProdName like @GoodsName ");
                dynamicParams.Add("GoodsName", '%' + requset.GoodsName + '%');
            }






            sql.AppendFormat(@"SELECT count(1)
FROM `eagles`.`tb_order` a  where 1=1  {0} ;
 ", parameter);
            totalCount = dbManager.ExecuteScalar<int>(sql.ToString(), dynamicParams);

            sql.Clear();

            dynamicParams.Add("pageStart", (requset.PageNumber - 1) * requset.PageSize);
            dynamicParams.Add("pageNum", requset.PageNumber);
            dynamicParams.Add("pageSize", requset.PageSize);

            sql.AppendFormat(@"  SELECT a.OrgId,
    a.OrderId,
    a.ProdId,
    a.ProdName,
    a.OrderStatus,
    a.Score,
    a.Count,
    a.UserId,
    a.ExpressId,
    a.Address,
    a.Province,
    a.City,
    a.District,
    a.CreateTime,
    a.UpdateTime,
    a.OperId,b.Name,b.phone
FROM eagles.tb_order a
inner join eagles.tb_user_info b on a.UserId=b.UserId
  where  1=1  {0}     order by a.CreateTime desc limit  @pageStart ,@pageSize
 ", parameter);


//            sql.AppendFormat(@" SELECT `tb_order`.`OrgId`,
//    `tb_order`.`OrderId`,
//    `tb_order`.`ProdId`,
//    `tb_order`.`ProdName`,
//    `tb_order`.`OrderStatus`,
//    `tb_order`.`Score`,
//    `tb_order`.`Count`,
//    `tb_order`.`UserId`,
//    `tb_order`.`ExpressId`,
//    `tb_order`.`Address`,
//    `tb_order`.`Province`,
//    `tb_order`.`City`,
//    `tb_order`.`District`,
//    `tb_order`.`CreateTime`,
//    `tb_order`.`UpdateTime`,
//    `tb_order`.`OperId`
//FROM `eagles`.`tb_order`
//  where  1=1  {0}  
// ", parameter);

            return dbManager.Query<TbOrder>(sql.ToString(), dynamicParams);
        }

        public TbOrder GetOrderDetail(GetDeliverGoodsDetailRequset requset)
        {
            var sql = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            sql.Append(@" SELECT `tb_order`.`OrgId`,
    `tb_order`.`OrderId`,
    `tb_order`.`ProdId`,
    `tb_order`.`ProdName`,
    `tb_order`.`OrderStatus`,
    `tb_order`.`Score`,
    `tb_order`.`Count`,
    `tb_order`.`UserId`,
    `tb_order`.`ExpressId`,
    `tb_order`.`Address`,
    `tb_order`.`Province`,
    `tb_order`.`City`,
    `tb_order`.`District`,
    `tb_order`.`CreateTime`,
    `tb_order`.`UpdateTime`,
    `tb_order`.`OperId`
FROM `eagles`.`tb_order`
  where OrderId=@OrderId;
 ");
            dynamicParams.Add("OrderId", requset.OrderId);

            return dbManager.QuerySingle<TbOrder>(sql.ToString(), dynamicParams);
        }

        public int EditOrder(List<TbOrder> mod)
        {
            return dbManager.Excuted(@" UPDATE `eagles`.`tb_order`
SET
`ExpressId` =@ExpressId,
`Address` =@Address
WHERE `OrderId` =@OrderId;

 ", mod);
        }
    }
}
